﻿using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using ZBJF.Warranty.Common.Common;
using ZBJF.Warranty.Common.Enums.InfoCollect;
using ZBJF.Warranty.Model;
using ZBJF.Warranty.Model.ViewModel.BaseQueryModel;
using ZBJF.Warranty.Model.ViewModel.InfoCollect;

namespace ZBJF.Warranty.BLL
{
    public partial class InsuranceProductsInfoCollectItemMappingsService
    {
        public InsuranceProductsInfoCollectItemMappings LoadByID(int id)
        {
            return LoadEntities(o => o.ID == id).FirstOrDefault();
        }
       
        public bool Delete(int id)
        {
            var entity = LoadByID(id);
            if (entity != null)
            {
                return DeleteEntities(entity);
            }
            return false;
        }
        public IQueryable<InsuranceProductsInfoCollectItemMappings> GetPagingList(InsuranceProductsInfoCollectItemMappingsModel model, out int total)
        {
            var expressionWhere = PredicateExtensions.True<InsuranceProductsInfoCollectItemMappings>();
            if (model.CellPhoneServiceProjectID > 0)
            {
                expressionWhere = expressionWhere.And(o => o.CellPhoneServiceProjectID == model.CellPhoneServiceProjectID);
            }
            return LoadPagerEntities<InsuranceProductsInfoCollectItemMappings>(model.PageIndex, model.iDisplayLength, out total, expressionWhere, model.iSorts);
        }
        public List<InsuranceProductInfoCollectItemSnapshoot> LoadInfoCollectItems(List<int> cellPhoneServiceProjectIDs, byte infoCollectWith = 0)
        {
            var expressionWhere = PredicateExtensions.True<InsuranceProductsInfoCollectItemMappings>();
            expressionWhere = expressionWhere.And(o => cellPhoneServiceProjectIDs.Contains(o.CellPhoneServiceProjectID));
            if (infoCollectWith > 0)
                expressionWhere =
                    expressionWhere.And(o => o.CollectTypeCollectItemMappings.CollectWithType == infoCollectWith);
            return LoadEntities(expressionWhere).AsNoTracking().ToList().Select(o => new InsuranceProductInfoCollectItemSnapshoot
            {
                InfoCollectWith = infoCollectWith,
                CanUploadPicCount = o.CanUploadPicCount,
                InfoCollectCategoryName = o.CollectTypeCollectItemMappings.InsuranceProductsInfoCollectItem.InsuranceProductsInfoCollectCategory.InfoCollectCategoryName,
                InfoCollectItemID = o.CollectTypeCollectItemMappings.InsuranceProductsInfoCollectItem.ID,
                InfoCollectItemName = o.CollectTypeCollectItemMappings.InsuranceProductsInfoCollectItem.InfoCollectItemName,
                IsMust = o.IsMust,
                InfoCollectWithName = EnumHelper.Instance.GetDescription(typeof(CollectWithType), infoCollectWith)
            }).ToList();
        }
    }
}